package org.shock.ss.sms.commons.dao;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import org.shock.ss.sms.commons.dao.impl.BaseDaoImpl;

/**
 * dao工厂类 从daocontext.properties来动态加载相应的实现类
 * 
 * @author 季乐
 * 
 */
public class DaoFactory {
	@SuppressWarnings("unchecked")
	private static Map<String, BaseDao> daoMap = new HashMap<String, BaseDao>();
	private static Logger log = Logger.getLogger(DaoFactory.class);
	private static Properties p = new Properties();
	static {
		try {
			p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("daocontext.properties"));
		} catch (IOException e) {
			log.error(e, e);
		}
	}

	// 返回一个dao实例
	public static BaseDao getDao(String daoName) {
		BaseDao dao = null;
		try {
			if (daoMap.get(daoName) != null) {
				return daoMap.get(daoName);
			}
			dao = (BaseDaoImpl<?>) Class.forName(p.getProperty(daoName)).newInstance();
			daoMap.put(daoName, dao);
		} catch (Exception e) {
			log.error(e, e);
		}
		log.debug(dao);
		return dao;
	}
}
